package com.cw.jeeyt.service.impl.security;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityOperateLogBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.security.SecurityOperateLogDao;
import com.cw.jeeyt.dao.model.security.SecurityOperateLogDo;
import com.cw.jeeyt.service.converter.security.SecurityOperateLogConverter;
import com.cw.jeeyt.service.intf.security.SecurityOperateLogService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 操作日志信息业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SecurityOperateLogServiceImpl implements SecurityOperateLogService {

    private static final Logger logger = LoggerFactory.getLogger(SecurityOperateLogServiceImpl.class);
    @Autowired
    private SecurityOperateLogDao securityOperateLogDao;

    /**
     * 分页查询操作日志信息
     *
     * @param securityOperateLogBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SecurityOperateLogBo> queryByPage(SecurityOperateLogBo securityOperateLogBo, Integer startNum,
        Integer pageSize) throws Exception {

        if (securityOperateLogBo == null) {
            logger.error("操作日志信息为空");
            return null;
        }
        PageInfo<SecurityOperateLogDo> pageInfo = securityOperateLogDao.queryByPage(
            SecurityOperateLogConverter.bo2Do(securityOperateLogBo), startNum, pageSize);
        List<SecurityOperateLogBo> securityOperateLogBos = SecurityOperateLogConverter.doList2BoList(
            pageInfo.getList());
        if (CollectionUtil.isNotEmpty(securityOperateLogBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("operateType", "operate_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(securityOperateLogBos)),
                dictMap);
            securityOperateLogBos = JSON.parseArray(jsonArray.toJSONString(), SecurityOperateLogBo.class);
        }
        return new PageInfo<>(pageInfo, securityOperateLogBos);
    }

    /**
     * 根据主键查寻操作日志信息
     *
     * @param id
     * @return
     */
    @Override
    public SecurityOperateLogBo getSecurityOperateLogById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return SecurityOperateLogConverter.do2Bo(securityOperateLogDao.getById(id));
    }

    /**
     * 新增操作日志信息
     *
     * @param securityOperateLogBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SecurityOperateLogBo securityOperateLogBo) throws Exception {

        if (securityOperateLogBo == null) {
            logger.error("操作日志信息为空");
            return null;
        }
        SecurityUserBo user = SessionUtil.getSessionUser();
        SecurityOperateLogDo securityOperateLog = SecurityOperateLogConverter.bo2Do(securityOperateLogBo);
        securityOperateLog.setId(SequenceTool.nextId());
        if (user == null) {
            securityOperateLog.setCreateBy(100L);
            securityOperateLog.setUpdateBy(100L);
        } else {
            securityOperateLog.setUpdateBy(user.getId());
            securityOperateLog.setCreateBy(user.getId());
        }
        securityOperateLog.setCreateDate(new Date());
        securityOperateLog.setOperateTime(new Date());//操作时间
        securityOperateLog.setUpdateDate(new Date());
        int i = securityOperateLogDao.insert(securityOperateLog);
        return i;
    }

    /**
     * 更新操作日志信息
     *
     * @param securityOperateLogBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SecurityOperateLogBo securityOperateLogBo) throws Exception {

        if (securityOperateLogBo == null) {
            logger.error("操作日志信息为空");
            return;
        }
        SecurityOperateLogDo securityOperateLog = SecurityOperateLogConverter.bo2Do(securityOperateLogBo);
        securityOperateLog.setUpdateBy(100L);
        securityOperateLog.setUpdateDate(new Date());
        securityOperateLogDao.update(securityOperateLog);
    }

    /**
     * 删除操作日志信息
     *
     * @param securityOperateLogBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SecurityOperateLogBo securityOperateLogBo) throws Exception {

        if (securityOperateLogBo == null) {
            logger.error("操作日志信息为空");
            return;
        }
        securityOperateLogDao.delete(SecurityOperateLogConverter.bo2Do(securityOperateLogBo));
    }
}
